<?php
require_once( 'mofachi.DataConnection.php' );
class UserData {
	private $field = array();
	public function __get( $key ) {
		if( !isset( $this->field[ $key ] ) ) {
            return null;
        }
		return $this->field[ $key ];
	}
	public function __set( $key, $value ) {
		$this->field[ $key ] = $value;
	}
	public function __construct() {
		$this->field[ "user_id" ] = null;
		$this->field[ "email" ] = null;
		$this->field[ "password" ] = null;
		$this->field[ "nickname" ] = null;
		$this->field[ "realname" ] = null;
		$this->field[ "gender" ] = null;
		$this->field[ "location_id" ] = null;
		$this->field[ "birthday" ] = null;
		$this->field[ "reg_time" ] = null;
		$this->field[ "edit_time" ] = null;
	}
	public function add() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "insert into user values( '', '".$this->field[ "email" ]."', password('".$this->field[ "password" ]."'));";
		$query.= "select user_id from user where email='".$this->field[ "email" ]."'";
		if( !$db->multi_query( $query ) ) {
			$db->close();
			return false;
		}
		if( !$db->more_results() || !$db->next_result() ) {
			$db->close();
			return false;
		}		
		$result = $db->store_result();
		$u = $result->fetch_assoc();
		$this->field[ "user_id" ] = $u[ "user_id" ];

		$result->free();
		$db->close();
		return true;
	}
	public function addInfo() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "insert into userinfo values( ".$this->field[ "user_id" ].", '".$this->field[ "nickname" ];
		$query.= "','".$this->field[ "realname" ]."','".$this->field[ "gender" ]."', '".$this->field[ "location_id" ];
		$query.= "','".$this->field[ "birthday" ]."',now(),now())";

		if( !$db->query( $query ) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	private function loadData( $row ) {
		foreach( $this->field as $key => $value ) {
			$this->field[ $key ] = $row[ $key ];
		}
	}
	public function loadByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user where user_id=".$this->field[ "user_id" ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$row = $result->fetch_assoc();
		$this->field[ "email" ] = $row[ "email" ];
		$this->field[ "password" ] = $row[ "password" ];

		$result->free();
		$db->close();
		return true;
	}
	public function loadInfoByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select u.user_id,u.email,u.password,ui.nickname,ui.realname,ui.gender,ui.location_id,";
		$query.= "ui.birthday,ui.reg_time,ui.edit_time from user u left join userinfo ui ";
		$query.= "on u.user_id=ui.user_id where u.user_id=".$this->field[ "user_id" ];

		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;		
	}
	public function loadByEmail() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user where email='".$this->field[ "email" ]."'";
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$row = $result->fetch_assoc();
		$this->field[ "user_id" ] = $row[ "user_id" ];
		$this->field[ "password" ] = $row[ "password" ];

		$result->free();
		$db->close();
		return true;		
	}
	public function loadInfoByEmail() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select u.user_id,u.email,u.password,ui.nickname,ui.realname,ui.gender,ui.location_id,";
		$query.= "ui.birthday,ui.reg_time,ui.edit_time from user u left join userinfo ui ";
		$query.= "on u.user_id=ui.user_id where u.email='".$this->field[ "email" ]."'";

		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;		
	}
	public function loadInfoByNickname() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select u.user_id,u.email,u.password,ui.nickname,ui.realname,ui.gender,ui.location_id,";
		$query.= "ui.birthday,ui.reg_time,ui.edit_time from user u left join userinfo ui ";
		$query.= "on u.user_id=ui.user_id where u.nickname='".$this->field[ "nickname" ]."'";

		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;		
	}
	public function update() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "update user set email='".$this->field[ "email" ]."', password=('";
		$query.= $this->field[ "password" ]."') where user_id=".$this->field[ "user_id" ];
		if( !$db->query( $query ) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function updateInfo() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "update userinfo set nickname='".$this->field[ "nickname" ];
		$query.= "', realname='".$this->field[ "realname" ]."', gender=".$this->field[ "gender" ];
		$query.= ", location_id=".$this->field[ "location_id" ].", birthday=date('";
		$query.= $this->field[ "birthday" ]."'), edit_time=now() where user_id=".$this->field[ "user_id" ];
		if( !$db->query( $query ) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function remove() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$db->query( "delete from user where user_id=".$this->field[ "user_id" ] );
		$db->query( "delete from userinfo where user_id=".$this->field[ "user_id" ] );
		$db->close();
		return true;
	}
	public function getBabies() {
		if( !$this->user_id ) {
			return false;
		}
		require_once( 'mofachi.UserBabyData.php' );
		$userBaby = new UserBabyData();
		$userBaby->user_id = $this->field[ "user_id" ];
		return $userBaby->loadByUserID();
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------
	public static function authenticate( $email, $password ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from user where email='".$email."' and password=password('".$password."')";
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$result->free();
		$db->close();
		return true;
	}
	public static function listUser( $offset, $length ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "select * from user order by user_id desc limit ".$offset.",".$length;
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$returnArray = array();
		$num = $result->num_rows;
		for( $i = 0; $i < $num; $i++ ) {
			$user = new UserData();
			$row = $result->fetch_assoc();
			$user->user_id = $row[ "user_id" ];
			$user->email = $row[ "email" ];
			$user->password = $row[ "password" ];
			$returnArray[ $i ] = $user;
		}
		$result->free();
		$db->close();
		return $returnArray;
	}
	public static function listUserInfo( $offset, $length ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "select u.user_id,u.email,u.password,ui.nickname,ui.realname,ui.gender,ui.location_id,";
		$query.= "ui.birthday, ui.reg_time,ui.edit_time from user u left join userinfo ui ";
		$query.= "on u.user_id=ui.user_id order by user_id desc limit ".$offset.",".$length;
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;
		}
		$returnArray = array();
		$num = $result->num_rows;
		for( $i = 0; $i < $num; $i++ ) {
			$user = new UserData();
			$user->loadData( $result->fetch_assoc() );
			$returnArray[ $i ] = $user;
		}
		$result->free();
		$db->close();
		return $returnArray;
	}
}
?>